<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css" />
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" lay-verify="name" autocomplete="off" placeholder="请输入标题" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">描述</label>
                    <div class="layui-input-block">
                        <textarea name="des" id="des" class="layui-input"></textarea>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">图片</label>
                    <div class="layui-input-block">
                      <input type="hidden" name="img">
                      <button type="button" class="layui-btn" id="img">上传图片</button>
                      <div class="layui-upload-list">
                        <img class="layui-upload-img" id="demo1">
                        <p id="demoText"></p>
                      </div>
                    </div>
                </div>   

            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit="" lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>

<script src="/component/layui/layui.all.js"></script>
<script src="/component/pear/pear.js"></script>
<script>
layui.use(['upload','jquery'], function(){
  var upload = layui.upload;
  var $=layui.jquery;
  var MODULE_PATH = "{$controller}";
  //执行实例
  var uploadInst = upload.render({
    elem: '#img' //绑定元素
    ,url: '/'+MODULE_PATH+'/upload' //上传接口
    ,done: function(res){
        //上传完毕回调
        if(res.code==1){
            $('[name="img"]').val(res.savename);
        }
    }
    ,error: function(){
      //请求异常回调
    }
  });
});
</script>
<script>
var modules = [
    'form','jquery','laydate','uploads'
];
layui.use(modules,function(){
    let form = layui.form;
    let $ = layui.jquery;
    let laydate = layui.laydate;

    //常规用法
    
    form.on('submit(user-save)', function(data){
        let roleIds = "";
        $('input[type=checkbox]:checked').each(function() {
            roleIds += $(this).val()+",";
        });
        roleIds = roleIds.substr(0,roleIds.length-1);
        data.field.roleIds = roleIds;

        $.ajax({
            url:'/{$controller}/create',
            data:JSON.stringify(data.field),
            dataType:'json',
            contentType:'application/json',
            type:'post',
            success:function(result){
                if(result.code==1){
                    layer.msg(result.msg,{icon:1,time:1000},function(){
                        parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                        parent.layui.table.reload("user-table");
                    });
                }else{
                    layer.msg(result.msg,{icon:2,time:1000});
                }
            }
        })
        
        return false;
    });
})


</script>
<script>
</script>
</body>
</html>